package sort;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * @author XY
 * @version 1.0
 * @date 2022/2/15 9:32
 * @Description
 */
public class Leet888 {
    public int[] fairCandySwap(int[] aliceSizes, int[] bobSizes) {
        int sum1 = Arrays.stream(aliceSizes).sum();
        int sum2 = Arrays.stream(bobSizes).sum();
        for (int i = 0; i < aliceSizes.length; i++) {
            for (int j = 0; j < bobSizes.length; j++) {
                if((sum1+bobSizes[j]-aliceSizes[i])==(sum2+aliceSizes[i]-bobSizes[j])){
                    return new int[] {aliceSizes[i],bobSizes[j]};
                }
            }
        }
        return null;
    }

    //sum1-x+y = sum2-y+x  x= y+(sum1-sum2)/2
    public int[] fairCandySwap2(int[] aliceSizes, int[] bobSizes) {
        int sumA = Arrays.stream(aliceSizes).sum();
        int sumB = Arrays.stream(bobSizes).sum();
        int delta = (sumA - sumB) / 2;
        Set<Integer> rec = new HashSet<Integer>();
        for (int num : aliceSizes) {
            rec.add(num);
        }
        int[] ans = new int[2];
        for (int y : bobSizes) {
            int x = y + delta;
            if (rec.contains(x)) {
                ans[0] = x;
                ans[1] = y;
                break;
            }
        }
        return ans;
    }
}
